/*******************************************************************************
* Project: QSAR(Cute Software Architecture) 
*
* Copyright: Copyright(C) 2024 by YuetingBen, All Rights Reserved
*
* File name: DcmDspSvc10.c
*
* Brief: ;
*
* Author: Yueting.Ben
*
* Module: Dcm
*
* Revision:
* --------------------------------------------------------------------------------
* v0.1: 
* --Yueting.Ben, 2024-03-14 
* --Initial release

**********************************************************************************/

/** HEADER FILES **/
#include "DcmDspSvc10.h"
#include "DcmDslCfg.h"
#include "DcmDsp.h"



/** MACRO DEFINITION **/
/* The following index refer to Dcm_MsgContextType reqData and resData, start after service identifier(which is not part
of this data) */
#define DCM_REQ_SUBID_INDEX    0u


/** TYPEDEF DEFINITION **/


/** LOCAL DATA DECLARATION **/


/** GLOBAL DATA DECLARATION **/


/** LOCAL FUNCTION DECLARATION **/
Std_ReturnType DcmDsp_SessionCheck(
    DcmDspSessionType* sesPrt
);

Std_ReturnType DcmDsp_Svc10_Processor(
    Dcm_ExtendedOpStatusType OpStatus,
    Dcm_MsgContextType* pMsgContext,
    Dcm_NegativeResponseCodeType* ErrorCode
);

Std_ReturnType DcmDsp_Svc11_Processor(
    Dcm_ExtendedOpStatusType OpStatus,
    Dcm_MsgContextType* pMsgContext,
    Dcm_NegativeResponseCodeType* ErrorCode
);

Std_ReturnType DcmDsp_Svc10_01_Processor(
    Dcm_ExtendedOpStatusType OpStatus,
    Dcm_MsgContextType* pMsgContext,
    Dcm_NegativeResponseCodeType* ErrorCode
);
Std_ReturnType DcmDsp_Svc10_02_Processor(
    Dcm_ExtendedOpStatusType OpStatus,
    Dcm_MsgContextType* pMsgContext,
    Dcm_NegativeResponseCodeType* ErrorCode
);

Std_ReturnType DcmDsp_Svc10_03_Processor(
    Dcm_ExtendedOpStatusType OpStatus,
    Dcm_MsgContextType* pMsgContext,
    Dcm_NegativeResponseCodeType* ErrorCode
);

/** GLOBAL FUNCTION DECLARATION **/


/** LOCAL DATA **/


/** GLOBAL DATA **/


/** LOCAL FUNCTION **/

/*******************************************************************************
* -Name            DcmDsp_SessionCheck
* -Brief           None
* -Details         None
* -Param[in]       OpStatus: The operations status        
* -Param[in, out]  pMsgContext: Message context 
* -Param[out]      ErrorCode: Negative response code
* -Return          E_OK:          
*                  E_NOT_OK: 
*                  DCM_E_PENDING:
*                  DCM_E_FORCE_RCRRP:
*******************************************************************************/
Std_ReturnType DcmDsp_SessionCheck(
    DcmDspSessionType* sesPrt
)
{
    Std_ReturnType retVal = E_OK;
    Dcm_SesCtrlType sesLevel;
    uint8 sesIndex;

    DcmDsl_GetSesCtrlType(&sesLevel);
    for(sesIndex = 0u; sesIndex < DCM_SESSION_MAX; sesIndex++)
    {
        if(NULL_PTR != sesPrt->DcmDspSessionCfg[sesIndex])
        {
            if((sesLevel == sesPrt->DcmDspSessionCfg[sesIndex]->sessionLevel)
              || (DCM_DEFAULT_SESSION == sesPrt->DcmDspSessionCfg[sesIndex]->sessionLevel))
            {
                retVal = E_OK;
                break;
            }
            else
            {
                retVal = E_NOT_OK;
            }
        }
    }
    
    return(retVal);
}


/*******************************************************************************
* -Name            DcmDsp_Svc10_Processor
* -Brief           Processor for DiagnosticSessionControl (10 hex) service 
* -Details         None
* -Param[in]       OpStatus: The operations status        
* -Param[in, out]  pMsgContext: Message context 
* -Param[out]      ErrorCode: Negative response code
* -Return          E_OK:          
*                  E_NOT_OK: 
*                  DCM_E_PENDING:
*                  DCM_E_FORCE_RCRRP:
*******************************************************************************/
Std_ReturnType DcmDsp_Svc10_Processor(
    Dcm_ExtendedOpStatusType OpStatus,
    Dcm_MsgContextType* pMsgContext,
    Dcm_NegativeResponseCodeType* ErrorCode
)
{
    Std_ReturnType retVal = E_OK;

    return(retVal);
}


/*******************************************************************************
* -Name            DcmDsp_Svc10_01_Processor
* -Brief           Processor for DiagnosticSessionControl (10 hex) sub service 
* -Details         None
* -Param[in]       OpStatus: The operations status        
* -Param[in, out]  pMsgContext: Message context 
* -Param[out]      ErrorCode: Negative response code
* -Return          E_OK:          
*                  E_NOT_OK: 
*******************************************************************************/
Std_ReturnType DcmDsp_Svc10_01_Processor(
    Dcm_ExtendedOpStatusType OpStatus,
    Dcm_MsgContextType* pMsgContext,
    Dcm_NegativeResponseCodeType* ErrorCode
)
{
    Std_ReturnType retVal = E_OK;
    Dcm_SesCtrlType sesCtrlType;
    DcmDspSessionCfgType* sessionCfgPtr;

    sesCtrlType = (Dcm_SecLevelType)(pMsgContext->reqData[DCM_REQ_SUBID_INDEX]);

    switch(Dcm_GetProgress(pMsgContext))
    {
        case DCM_PROGRESS_INITIAL:
        {
            retVal = DCM_E_PENDING;
            Dcm_SetProgress(pMsgContext, DCM_PROGRESS_LENGTH_FORMAT_CHECK);
            break;
        }
        case DCM_PROGRESS_LENGTH_FORMAT_CHECK:
        {
            retVal = DCM_E_PENDING;
            if(DCMDSPSVC10_REQ_DATA_MINLENGTH == pMsgContext->reqDataLen)
            {
                Dcm_SetProgress(pMsgContext, DCM_PROGRESS_IMPLEMENTATION);
            }
            else
            {
                *ErrorCode = DCM_E_INCORRECTMES_SAGELENGTHORINVALID_FORMAT;
                retVal = E_NOT_OK;
            }
            break;
        }
        case DCM_PROGRESS_IMPLEMENTATION:
        {
            sessionCfgPtr = (DcmDspSessionCfgType*)DcmDspSessionCfg[0u];
            pMsgContext->resData[1] = (uint8)(sessionCfgPtr->sessionP2ServerMax >> 8);
            pMsgContext->resData[2] = (uint8)(sessionCfgPtr->sessionP2ServerMax);
            pMsgContext->resData[3] = (uint8)((sessionCfgPtr->sessionP2StarServerMax / 10) >> 8);
            pMsgContext->resData[4] = (uint8)(sessionCfgPtr->sessionP2StarServerMax / 10);
            pMsgContext->resDataLen = 5;

            DcmDsl_SetSesCtrlType(sesCtrlType);
            retVal = E_OK;

            break;
        }        
    }

    return(retVal);
}

/*******************************************************************************
* -Name            DcmDsp_Svc10_02_Processor
* -Brief           Processor for DiagnosticSessionControl (10 hex) sub service 
* -Details         None
* -Param[in]       OpStatus: The operations status        
* -Param[in, out]  pMsgContext: Message context 
* -Param[out]      ErrorCode: Negative response code
* -Return          E_OK:          
*                  E_NOT_OK: 
*******************************************************************************/
Std_ReturnType DcmDsp_Svc10_02_Processor(
    Dcm_ExtendedOpStatusType OpStatus,
    Dcm_MsgContextType* pMsgContext,
    Dcm_NegativeResponseCodeType* ErrorCode
)
{
    Std_ReturnType retVal = E_OK;
    Dcm_SesCtrlType sesCtrlType;
    DcmDspSessionCfgType* sessionCfgPtr;

    sesCtrlType = (Dcm_SecLevelType)(pMsgContext->reqData[DCM_REQ_SUBID_INDEX]);

    switch(Dcm_GetProgress(pMsgContext))
    {
        case DCM_PROGRESS_INITIAL:
        {
            retVal = DCM_E_PENDING;
            Dcm_SetProgress(pMsgContext, DCM_PROGRESS_LENGTH_FORMAT_CHECK);
            break;
        }
        case DCM_PROGRESS_LENGTH_FORMAT_CHECK:
        {
            retVal = DCM_E_PENDING;
            if(DCMDSPSVC10_REQ_DATA_MINLENGTH == pMsgContext->reqDataLen)
            {
                Dcm_SetProgress(pMsgContext, DCM_PROGRESS_IMPLEMENTATION);
            }
            else
            {
                *ErrorCode = DCM_E_INCORRECTMES_SAGELENGTHORINVALID_FORMAT;
                retVal = E_NOT_OK;
            }
            break;
        }
        case DCM_PROGRESS_IMPLEMENTATION:
        {
            sessionCfgPtr = (DcmDspSessionCfgType*)DcmDspSessionCfg[1u];
            pMsgContext->resData[1] = (uint8)(sessionCfgPtr->sessionP2ServerMax >> 8);
            pMsgContext->resData[2] = (uint8)(sessionCfgPtr->sessionP2ServerMax);
            pMsgContext->resData[3] = (uint8)((sessionCfgPtr->sessionP2StarServerMax / 10) >> 8);
            pMsgContext->resData[4] = (uint8)(sessionCfgPtr->sessionP2StarServerMax / 10);
            pMsgContext->resDataLen = 5;

            DcmDsl_SetSesCtrlType(sesCtrlType);
            retVal = E_OK;

            break;
        }        
    }

    return(retVal);
}



/*******************************************************************************
* -Name            DcmDsp_Svc10_03_Processor
* -Brief           Processor for DiagnosticSessionControl (10 hex) sub service 
* -Details         None
* -Param[in]       OpStatus: The operations status        
* -Param[in, out]  pMsgContext: Message context 
* -Param[out]      ErrorCode: Negative response code
* -Return          E_OK:          
*                  E_NOT_OK: 
*******************************************************************************/
Std_ReturnType DcmDsp_Svc10_03_Processor(
    Dcm_ExtendedOpStatusType OpStatus,
    Dcm_MsgContextType* pMsgContext,
    Dcm_NegativeResponseCodeType* ErrorCode
)
{
    Std_ReturnType retVal = E_OK;
    Dcm_SesCtrlType sesCtrlType;
    DcmDspSessionCfgType* sessionCfgPtr;

    sesCtrlType = (Dcm_SecLevelType)(pMsgContext->reqData[DCM_REQ_SUBID_INDEX]);

    switch(Dcm_GetProgress(pMsgContext))
    {
        case DCM_PROGRESS_INITIAL:
        {
            retVal = DCM_E_PENDING;
            Dcm_SetProgress(pMsgContext, DCM_PROGRESS_LENGTH_FORMAT_CHECK);
            break;
        }
        case DCM_PROGRESS_LENGTH_FORMAT_CHECK:
        {
            retVal = DCM_E_PENDING;
            if(DCMDSPSVC10_REQ_DATA_MINLENGTH == pMsgContext->reqDataLen)
            {
                Dcm_SetProgress(pMsgContext, DCM_PROGRESS_IMPLEMENTATION);
            }
            else
            {
                *ErrorCode = DCM_E_INCORRECTMES_SAGELENGTHORINVALID_FORMAT;
                retVal = E_NOT_OK;
            }
            break;
        }
        case DCM_PROGRESS_IMPLEMENTATION:
        {
            sessionCfgPtr = (DcmDspSessionCfgType*)DcmDspSessionCfg[2u];
            pMsgContext->resData[1] = (uint8)(sessionCfgPtr->sessionP2ServerMax >> 8);
            pMsgContext->resData[2] = (uint8)(sessionCfgPtr->sessionP2ServerMax);
            pMsgContext->resData[3] = (uint8)((sessionCfgPtr->sessionP2StarServerMax / 10) >> 8);
            pMsgContext->resData[4] = (uint8)(sessionCfgPtr->sessionP2StarServerMax / 10);
            pMsgContext->resDataLen = 5;

            DcmDsl_SetSesCtrlType(sesCtrlType);
            retVal = E_OK;

            break;
        }        
    }

    return(retVal);
}


/** GLOBAL FUNCTION **/

